{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "555fe16d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ssd', 'sss']\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "type object 'person' has no attribute 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-32-6f64601d0dc2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     16\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'%s is %s weights %s'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhobbies\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[0mperson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhobbies2\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'smokking'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'ml'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'drinking'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: type object 'person' has no attribute 'name'"
     ]
    }
   ],
   "source": [
    "# 这三种方法的主要区别在于参数，实例方法被绑定到一个实例，只能通过实例进行调用；但是对于静态方法和类方法，\n",
    "# 可以通过类名和实例两种方式进行调用。\n",
    "class person(object):  # 基类\n",
    "    '''\n",
    "    person类的注释\n",
    "    '''\n",
    "    tall = 180\n",
    "    hobbies = ['ssd','sss']\n",
    "\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "\n",
    "    def infoma(self):\n",
    "        print('%s is %s weights %s' % (self.name, self.age, self.weight))\n",
    "print(person.hobbies)\n",
    "print(person.name)\n",
    "\n",
    "person.hobbies2=['smokking','ml','drinking']\n",
    "print(person.__name__)\n",
    "print(person.__doc__)\n",
    "print(person.__bases__)\n",
    "print(person.__dir__)\n",
    "print(person.__module__)\n",
    "print(person.__class__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "174c65cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'object'>\n",
      "person\n",
      "<class 'type'>\n"
     ]
    }
   ],
   "source": [
    "class person(object):\n",
    "\n",
    "    tall = 180\n",
    "    hobbies = []\n",
    "\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "\n",
    "    @classmethod\n",
    "    def infoma(cls):\n",
    "        print(cls.__base__)\n",
    "        print(cls.__name__)\n",
    "        print(cls.__class__)\n",
    "    \n",
    "\n",
    "\n",
    "# cls表示类本身\n",
    "person.infoma() #直接调用类的装饰器函数，通过cls可以访问类的相关属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a8acb928",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bruce is 25 weights 180\n",
      "180\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "def _infomation():\n",
    "    Bruce = person(\"Bruce\", 25, 180)  # 通过实例访问\n",
    "    Bruce.infomation()\n",
    "\n",
    "_infomation()\n",
    "\n",
    "class person(object):\n",
    "    tall = 180\n",
    "    hobbies = []\n",
    "\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "    def infomation(self):\n",
    "        print('%s is %s weights %s' % (self.name, self.age, self.weight))\n",
    "    @staticmethod  # 静态方法装饰器\n",
    "    def infoma():  # 没有参数限制，既不要实例参数，也不用类参数\n",
    "        print(person.tall)\n",
    "        print(person.hobbies)\n",
    "\n",
    "    @classmethod #类方法装饰器\n",
    "    def infoma1(cls):\n",
    "        print(cls.tall)\n",
    "        print(cls.hobbies)\n",
    "\n",
    "\n",
    "# person.infoma()   #静态法可以通过类名访问\n",
    "Bruce = person(\"Bruce\", 25, 180)  # 通过实例访问\n",
    "Bruce.infoma1()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "668fa4f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "430\n",
      "372929\n",
      "25\n",
      "26\n",
      "27\n"
     ]
    }
   ],
   "source": [
    "def _haha():\n",
    "    print('dd')\n",
    "\n",
    "\n",
    "def sum(a, b):\n",
    "    return a + b\n",
    "\n",
    "\n",
    "class person(object):\n",
    "    tall = 180\n",
    "    hobbies = []\n",
    "\n",
    "    def __init__(self, name, age, weight):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.weight = weight\n",
    "        self.__Id = 430\n",
    "    def get_ID(self):\n",
    "        return self.__Id\n",
    "    def set_ID(self,id):\n",
    "        self.__Id = id\n",
    "    def change_get(self,age):\n",
    "        ag = self.age\n",
    "        self.age = age\n",
    "        return ag\n",
    "    @staticmethod\n",
    "    def infoma():\n",
    "        print(person.tall)\n",
    "        print(person.hobbies)\n",
    "\n",
    "\n",
    "# person.infoma()\n",
    "Bruce = person(\"Bruce\", 25, 180)\n",
    "print(Bruce.age)\n",
    "#print(Bruce.__Id)\n",
    "print(Bruce.get_ID())\n",
    "Bruce.set_ID(372929)\n",
    "print(Bruce.get_ID())\n",
    "print(Bruce.change_get(Bruce.age + 1))\n",
    "print(Bruce.change_get(Bruce.age + 1))\n",
    "print(Bruce.age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3137bb52",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\"__“和” _ __\"的使用 更多的是一种规范/约定，并没有真正达到限制的目的：\n",
    "\n",
    "“_”：以单下划线开头的表示的是protected类型的变量，即只能允许其本身与子类进行访问；\n",
    "同时表示弱内部变量标示，如，当使用\"from moduleNmae import *\"时，不会将以一个下划线开头的对象引入。\n",
    "“__”：双下划线的表示的是私有类型的变量。只能是允许这个类本身进行访问了，连子类也不可以，\n",
    "这类属性在运行时属性名会加上单下划线和类名。\n",
    "'''\n",
    "def sm():\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f504ca6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "def xixi():\n",
    "    l = list(range(10))\n",
    "    print(l)\n",
    "xixi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a6426667",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "77 - 2 = 75\n",
      "True\n",
      "True\n",
      "True\n",
      "the bases are: (<class '__main__.Parent'>,)\n",
      "\n",
      "    parent class\n",
      "    \n",
      "None\n"
     ]
    }
   ],
   "source": [
    "class Parent(object):\n",
    "    '''\n",
    "    parent class\n",
    "    '''\n",
    "    numList = []\n",
    "    def numdiff(self, a, b):\n",
    "        return a-b\n",
    "\n",
    "class Child(Parent):\n",
    "    pass\n",
    "c = Child()    \n",
    "# subclass will inherit attributes from parent class \n",
    "#子类继承父类的属性   \n",
    "Child.numList.extend(range(10))\n",
    "print(Child.numList)\n",
    "\n",
    "print(\"77 - 2 =\", c.numdiff(77, 2))\n",
    "\n",
    "# built-in function issubclass() \n",
    "print(issubclass(Child, Parent))\n",
    "print(issubclass(Child, object))\n",
    "print(issubclass(Parent,object))\n",
    "# __bases__ can show all the parent classes\n",
    "#bases属性查看父类\n",
    "print('the bases are:',Child.__bases__)\n",
    "\n",
    "# doc string will not be inherited\n",
    "#doc属性不会被继承\n",
    "print(Parent.__doc__)\n",
    "print(Child.__doc__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb96694d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
